package com.lc.ibps.platform.rest.org;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.api.org.service.IPartyPositionMgrService;
import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 岗位管理控制器，第三方系统调用
 *
 * <pre>
 *  
 * 构建组：ibps-platform-webapi
 * 作者：Lium
 * 邮箱：1316679699@qq.com
 * 日期：2017年7月20日-下午2:26:26
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */
@RequestMapping(value = "/api/webapi/positionMgrService")
@Api(value = "/positionMgrService", tags = "用户管理-岗位管理服务")
@RestController
public class PositionMgrController extends BaseApiController{

	private IPartyPositionMgrService positionMgrService;

	public PositionMgrController() {
		positionMgrService = AppUtil.getBean(IPartyPositionMgrService.class);
	}

	/**
	 * 添加岗位信息
	 *
	 * @param json
	 *            {id:""/*选填/,name:"测试员"/*必填/,posAlias:"csy"/*必填/,levelID:
	 *            "84d780c0"/*必填/,
	 *            desc:"岗位描述"/*选填/,orgID:""/*选填/,parentId:""/*选填/,createTime:""/
	 *            *选填/}
	 */
	@ApiOperation(value = "添加岗位信息", notes = "添加岗位信息")
	@RequestMapping(value = "/createPosition", method = RequestMethod.PUT)
	public APIResult<String> createPosition(
			@ApiParam(value = "岗位信息json", required = true) @RequestParam(name = "json", required = true) String json) {
		APIResult<String> result = new APIResult<>();
		try {
			positionMgrService.createPosition(json); // TODO 数据不完整
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "添加岗位信息失败！", e);
		}
		return result;
	}

	/**
	 * 修改岗位信息
	 *
	 * @param json
	 *            {id:"257116e0-dec9-11e6-a540-9ba5bdd095ea"/*必填/,name:"测试员"/*必填
	 *            /,posAlias:"csy"/*必填/,
	 *            levelID:"84d780c0-dec8-11e6-a540-9ba5bdd095ea"/*必填/,desc:
	 *            "岗位描述"/*选填/,orgID:""/*选填/, parentId:""/*必填/,createTime:
	 *            "2017-01-20 12:30:33"/*必填/}
	 */
	@ApiOperation(value = "修改岗位信息", notes = "修改岗位信息")
	@RequestMapping(value = "/updatePosition", method = RequestMethod.POST)
	public APIResult<String> updatePosition(
			@ApiParam(value = "岗位信息json", required = true) @RequestParam(name = "json", required = true) String json) {
		APIResult<String> result = new APIResult<>();
		try {
			positionMgrService.updatePosition(json); // TODO 数据不完整
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "修改岗位信息失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据岗位ID删除岗位信息", notes = "根据岗位ID删除岗位信息")
	@RequestMapping(value = "/deletePosition", method = RequestMethod.DELETE)
	public APIResult<String> deletePosition(
			@ApiParam(value = "岗位ID", required = true) @RequestParam(name = "ids", required = true) String ids) {
		APIResult<String> result = new APIResult<>();
		try {
			positionMgrService.deletePosition(ids);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "删除岗位信息失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据岗位ID添加人员", notes = "根据岗位ID添加人员")
	@RequestMapping(value = "/addEmployee2Position", method = RequestMethod.PUT)
	public APIResult<String> addEmployee2Position(
			@ApiParam(value = "岗位ID", required = true) @RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "用户ID", required = true) @RequestParam(name = "users", required = true) String users) {
		APIResult<String> result = new APIResult<>();
		try {
			positionMgrService.addEmployee2Position(id, users);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "岗位添加人员失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据岗位ID添加角色", notes = "根据岗位ID添加角色")
	@RequestMapping(value = "/addRole2Position", method = RequestMethod.PUT)
	public APIResult<String> addRole2Position(
			@ApiParam(value = "岗位ID", required = true) @RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "角色ID", required = true) @RequestParam(name = "roles", required = true) String roles) {
		APIResult<String> result = new APIResult<>();
		try {
			positionMgrService.addRole2Position(id, roles);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "岗位添加角色失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据岗位ID添加组织", notes = "根据岗位ID添加组织")
	@RequestMapping(value = "/addOrg4Position", method = RequestMethod.PUT)
	public APIResult<String> addOrg4Position(
			@ApiParam(value = "岗位ID", required = true) @RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) String orgId) {
		APIResult<String> result = new APIResult<>();
		try {
			positionMgrService.addOrg4Position(id, orgId);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "岗位添加组织失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据岗位ID和用户ID添加负责人", notes = "根据岗位ID和用户ID添加负责人")
	@RequestMapping(value = "/addManager4Position", method = RequestMethod.PUT)
	public APIResult<String> addManager4Position(
			@ApiParam(value = "岗位ID", required = true) @RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) String userId) {
		APIResult<String> result = new APIResult<>();
		try {
			positionMgrService.addManager4Position(id, userId);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "岗位添加负责人失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据岗位ID移除人员", notes = "根据岗位ID移除人员")
	@RequestMapping(value = "/removeEmployee4Position", method = RequestMethod.DELETE)
	public APIResult<String> removeEmployee4Position(
			@ApiParam(value = "岗位ID", required = true) @RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "用户ID", required = true) @RequestParam(name = "users", required = true) String users) {
		APIResult<String> result = new APIResult<>();
		try {
			positionMgrService.removeEmployee4Position(id, users);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "岗位移除人员失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据岗位ID移除角色", notes = "根据岗位ID移除角色")
	@RequestMapping(value = "/removeRole4Position", method = RequestMethod.DELETE)
	public APIResult<String> removeRole4Position(
			@ApiParam(value = "岗位ID", required = true) @RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "角色ID", required = true) @RequestParam(name = "roles", required = true) String roles) {
		APIResult<String> result = new APIResult<>();
		try {
			positionMgrService.removeRole4Position(id, roles); // TODO 删除失败 没有删
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "岗位移除角色失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据用岗位ID移除组织", notes = "根据用岗位ID移除组织")
	@RequestMapping(value = "/removeOrg4Position", method = RequestMethod.DELETE)
	public APIResult<String> removeOrg4Position(
			@ApiParam(value = "岗位ID", required = true) @RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) String orgId) {
		APIResult<String> result = new APIResult<>();
		try {
			positionMgrService.removeOrg4Position(id, orgId);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "岗位移除组织失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据岗位ID和用户Id移除负责人", notes = "根据岗位ID和用户Id移除负责人")
	@RequestMapping(value = "/removeManager4Position", method = RequestMethod.DELETE)
	public APIResult<String> removeManager4Position(
			@ApiParam(value = "岗位ID", required = true) @RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) String userId) {
		APIResult<String> result = new APIResult<>();
		try {
			positionMgrService.removeManager4Position(id, userId);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "岗位移除负责人失败！", e);
		}
		return result;
	}
}
